import os

Import('env')
plf = {
    'le501x': 'arm_cm',
}
if env.get('IC') is None:
    env['IC'] = ARGUMENTS.get('ic','le501x')
env['BASE_ARCH'] = plf[env['IC']]
env['CPPDEFINES'] = [env['IC'].upper()]

def variant_dir_add_source(variant_dir,src_dir,src_list):
    env.VariantDir(variant_dir,src_dir,duplicate = 0)
    return File(env.subst(list(map(lambda x:os.path.join(variant_dir,x),src_list))))

def add_include(dir,path):
    inc_list = list(map(lambda x:os.path.relpath(env.subst(os.path.join(dir,x)),Dir('#').abspath),path))
    env.Append(CPPPATH = inc_list)
    return inc_list

def image_build(name,src,inc,ble=False,mesh=False,rtos = None,with_startup = True,objprefix = ''):
    src_files = []
    for i in src:
        if '$SDK_ROOT/' in i:
            env.VariantDir('build/$IC/','$SDK_ROOT/',duplicate = 0)
            src_files += [File(env.subst(os.path.join('build/$IC/',i.replace('$SDK_ROOT/',''))))]
        else:
            env.VariantDir('build/$IC/src','./',duplicate = 0)
            src_files += [File(env.subst(os.path.join('build/$IC/src',i)))]
    inc_list = []
    for i in inc:
        if '$SDK_ROOT/' in i:
            inc_list += [os.path.relpath(env.subst(i),Dir('#').abspath)]
        else:
            inc_list += [os.path.relpath(env.subst(os.path.join(Dir('.').path,i)),Dir('#').abspath)]
    env.Append(CPPPATH = inc_list)
    env.Append(LINKFLAGS = ' $GC_OPTION ')
    return env.plf_build(name,src_files,rtos,with_startup,ble,mesh,objprefix)

def app_build(app_name,src,inc,ble = True,mesh = False,rtos = None):
    target = image_build(app_name,src,inc,ble,mesh,rtos)
    env.plf_postaction(target)

env.variant_dir_add_source = variant_dir_add_source
env.add_include = add_include
env.image_build = image_build
env.app_build = app_build

SConscript(dirs = env['BASE_ARCH'], exports = ['env'])
flags = env.ParseFlags(ARGUMENTS.get('flags'))
env.MergeFlags(flags)

### default build parameters
default_opt_level = '-Os'
for v in flags['CCFLAGS']:
    print(v)
    if '-O' in v:
        default_opt_level = ''
        break
env.MergeFlags(default_opt_level)

